package com.hxkj.service.userCenter.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.AuditExpertServiceClient;
import com.hxkj.domain.po.AuditRecord;
import com.hxkj.domain.po.AuthUser;
import com.hxkj.domain.po.CertExpert;
import com.hxkj.domain.vo.ExpertAuditRecordVO;
import com.hxkj.service.userCenter.AuditExpertService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhangheng
 * @date 2018-10-11 16:49
 */
@Service
@Slf4j
public class AuditExpertServiceImpl implements AuditExpertService{
    @Autowired
    private AuditExpertServiceClient auditExpertServiceClient;
    @Autowired
    private TokenUtils tokenUtils;

    @Override
    public PageInfo<CertExpert> getList(CertExpert expert, PageTo pageParam) throws Exception {
        if(pageParam.getPageSize() == null || pageParam.getPageNum() == null){
            pageParam.setPageSize(10);
            pageParam.setPageNum(1);
        }
        ParameterDataStruct<CertExpert> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(expert);
        parameterDataStruct.setPageTo(pageParam);
        ResultDataStruct<PageInfo<CertExpert>> result = auditExpertServiceClient.getList(parameterDataStruct);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }

    @Override
    public CertExpert getInfo(CertExpert expert) throws Exception {
        if(expert.getId() == null){
            throw new CustomMessageException("认证专家编号不能为空");
        }
        ResultDataStruct<CertExpert> result = auditExpertServiceClient.getInfo(expert);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }

    @Override
    public ExpertAuditRecordVO getAuditList(CertExpert expert) throws Exception {
        if(expert.getId() == null){
            throw new CustomMessageException("认证专家编号不能为空");
        }
        ResultDataStruct<ExpertAuditRecordVO> result = auditExpertServiceClient.getAuditList(expert);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }

    @Override
    public void auditExpert(CertExpert expert) throws Exception {
        if(expert.getId() == null){
            throw new CustomMessageException("认证专家编号不能为空");
        }
        AuthUser user = (AuthUser) tokenUtils.getTokenUserInfo();
        AuditRecord auditRecord = new AuditRecord();
        auditRecord.setRefId(expert.getId());
        String status = expert.getStatus();
        if(StringUtils.isBlank(status)){
            throw new CustomMessageException("审核状态不能为空");
        }
        if("5".equals(status)){
            auditRecord.setResult(1);
            if(StringUtils.isBlank(expert.getReason())){
                throw new CustomMessageException("审核不通过必须填写原因");
            }
        }else if("10".equals(status)){
            auditRecord.setResult(0);
        }else{
            throw new CustomMessageException("只能选择审核成功或者审核失败");
        }
        auditRecord.setReason(expert.getReason());
        auditRecord.setAuditId(user.getUserId());
        auditRecord.setAuditor(user.getUserName());
        auditRecord.setCreateTime(System.currentTimeMillis());
        auditRecord.setUpdateTime(System.currentTimeMillis());
        auditRecord.setDelTag(0);
        //Map<String, Object> params = new HashMap<>();
        JSONObject params = new JSONObject();
        params.put("expert",expert);
        params.put("auditRecord",auditRecord);
        ResultMessageStruct result = auditExpertServiceClient.auditExpert(params);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }
}
